(function() {
    angular.module('app').component('productControllerComponent', {
        templateUrl: 'app/controllers/product.controller/product.controller.component/product.controller.component.html',
        controller: productControllerComponent,
        controllerAs: 'vm',
        bindings: {
            getType: '<',
            monitorProductAcount: '=' //bind monitor.controller
        }
    });

    productControllerComponent.$inject = ['productBasicService', '$scope', 'productControllerComponentService', '$loading','util','$controller'];

    function productControllerComponent(productBasicService, $scope, productControllerComponentService, $loading,util,$controller) {
        var vm = this;

        console.log('$$$$$$$$$');

        vm.$onInit = function(){
            
            angular.extend(vm, $controller('commonCtrl', {$scope: $scope}));//controller的扩张
            console.log(vm);

            vm.pager.pagerChange = pagerChange;

            pagerChange();
        };

        


        


        function pagerChange() {

            vm.param = util.extend(vm.param, vm.pager);

            //reset table
            $loading.start('tableProductLoading');
            //组件id 品牌商品数据
            vm.table.products.length = 0;

            productControllerComponentService.get(vm.param).then(function(data) {


                vm.pager.setAllPageCount(data.count);

                console.log(vm.pager);

                vm.table.products = data.products;

                productBasicService.shopBasicInit(); //店铺详情的序号
                productBasicService.brandBasicInit(); //店铺详情的序号
                productBasicService.monitorDataInit(); //日监控所在的序号

                $loading.finish('tableProductLoading');

            }, function(err) {
                vm.table.count = 0;
                vm.table.products.length = 0;
                $loading.finish('tableProductLoading');
            });

        }

        





        //vm.filter  //bind
        vm.table = {
            count: 0,
            products: []
        }


        //排序
        vm.sort = sort; //function


        //分页
        vm.pageChanged = pageChanged; //function

        //品牌详情
        vm.getBrandBasic = productBasicService.getBrandBasic; //function

        //店铺详情
        vm.getShopBasic = productBasicService.getShopBasic; //function

        //获取日监控
        vm.getMonitorData = productBasicService.getMonitorData; //function


        /*-----------------monitor.controller--------------------------*/
        //删除监控
        vm.deleteCollection = deleteCollection; //function



        
        active();

        function active() {

            // 组件bind $watch
            if (vm.filter) {

                var watchFun = $scope.$watch('vm.filter', function(newValue, oldValue) {

                    if ($.isEmptyObject(newValue)) {

                        // console.log('filter 还未初始化');
                        return;
                    }
                    // console.log('--------------------------------------------------------');

                    // console.log(newValue);

                    // console.log(oldValue);

                   

                    // if (angular.equals(newValue, oldValue)) {

                    //     console.log('vm.filter----------------------------------------$watch the same');
                    //     return;
                    // }

                    // console.log('vm.filter----------------------------------------$watch');
                    init();
                });

                $scope.$on('$destroy', function() {
                    // console.log('newProductControllerComponent');
                    watchFun();
                    // console.log(watchFun);
                });

            }

            if (vm.getType == 'monitorProduct') {
                // console.log('type:monitorProduct');
                monitorProductInit();
            }

        }

        function pageChanged(newPageNumber) {
            vm.pagination.newPageNumber = newPageNumber;
            vm.pagination.begin = (vm.pagination.newPageNumber - 1) * vm.pagination.count;
            init();
        }

        function init() {
            var json = $.extend({}, angular.copy(vm.filter), vm.pagination);

            //reset table
            $loading.start('tableProductLoading');
            //组件id 品牌商品数据
            vm.table.products.length = 0;

            productControllerComponentService.get(json).then(function(data) {


                vm.table.count = data.count / 10 * 11;

                vm.table.products = data.products;

                productBasicService.shopBasicInit(); //店铺详情的序号
                productBasicService.brandBasicInit(); //店铺详情的序号
                productBasicService.monitorDataInit(); //日监控所在的序号

                $loading.finish('tableProductLoading');

            }, function(err) {
                vm.table.count = 0;
                vm.table.products.length = 0;
                $loading.finish('tableProductLoading');
            });
        }




        function sort(sortColumn) {

            if (vm.pagination.sortColumn == sortColumn) {
                return;
            }
            vm.pagination.sortColumn = sortColumn;
            init();
        }


        /*-----------------------------monitor.controller---------------------------------*/
        function monitorProductInit() {
            $loading.start('tableProductLoading');
            //组件id 品牌商品数据
            vm.table.products.length = 0;
            productControllerComponentService.getCollection().then(function(data) {

                // console.log(data);
                if($.isEmptyObject(data)){
                    vm.table.products = [];

                    vm.monitorProductAcount = 0;
                    vm.table.count = 0;
                    $loading.finish('tableProductLoading');
                    return;
                }

                vm.table.count = data.collProducts.length;
                vm.table.products = data.collProducts;

                //bind
                vm.monitorProductAcount = data.collProducts.length;


                productBasicService.shopBasicInit(); //店铺详情的序号
                productBasicService.brandBasicInit(); //店铺详情的序号
                productBasicService.monitorDataInit(); //日监控所在的序号

                $loading.finish('tableProductLoading');

            }, function(err) {
                vm.table.count = 0;
                vm.table.products.length = 0;
                $loading.finish('tableProductLoading');
            });
        }


        function deleteCollection(prod) {
            productControllerComponentService.delCollection(prod).then(function() {
                var index = vm.table.products.indexOf(prod);
                // console.log("index:" + index);
                vm.table.products.splice(index, 1);
            });
        }

    }
})();
